#ifndef	WF_BUILDVOLT_GLOBALS
#define	WF_BUILDVOLT_GLOBALS
#include <includes.h>
#include "WF_BuildVolt.h"

static float AVRBeforeVolt_nwait,AVRAfterVolt_nwait;
static float AVRBeforeVolt_wait,AVRAfterVolt_wait;

static uint8_t ShutDownAlarm_WorkStepHandle(uint8_t *TempWFStep,uint8_t *TempWFPeriod,uint8_t *WFStaChange,uint8_t Solution)
{
  if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))//检查控制器的运行状态是否在停机告警态
  {
  	if(Solution&GenFaultWay_Action)
    {
        SetNewWorkFlowStepStatus(TempWFStep,TempWFPeriod,WFStaChange,BuildUpVoltageStep_endStep);
        return(DEF_TRUE);
    }
  }
  
  return(DEF_FALSE);
}

/**
 * @brief 建压流程
 * 建压电机到低限位
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_1st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_FALSE;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        if(PidReqBVolLimVal(20))
        {
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
            CurrProcCountdownEnable = DEF_TRUE;
        }
    }
    else
    {   
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
 
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_BVolLimVal])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_2ndStep);
        }
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}


/**
 * @brief 合闸前检查
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempOutAlarm 
 * @param WFStaChange 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_2st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE;
    ActInform |= SwitchBefCheckStep(TempWorkflow,TempWFStep,TempWFPeriod,BuildUpVoltageStep_Maximum,
        share_parameterCfgData_st.estaMagnCfg_st.EngineInitStart_time,share_parameterCfgData_st.estaMagnCfg_st.EngineInitSpeed,ShutDownAlarm_WorkStepHandle);
    return (ActInform);
}

/**
 * @brief 建压流程
 * 插入调压器并等待稳定
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempOutAlarm 
 * @param WFStaChange 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_3st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint16_t TempTimerVal;
    uint8_t ActInform = DEF_FALSE,WFStaChange=DEF_TRUE;

    /* Entry Moment */
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);

        TempTimerVal = 3;
        SMTimerCounter = (uint32_t)TempTimerVal * WorkFlowTimingFreq;
        // 插入调压器到AVR励磁电源和发电机之间（通过继电器闭合）
    	ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_BUILD_VOL_SW,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_RelayOut;
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        /* Time overflow */
        if (SMTimerCounter == 0)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
        }
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

/**
 * @brief 建压流程
 * 建压电机到预设初始值
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempOutAlarm 
 * @param WFStaChange 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_4st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_TRUE;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        if(PidReqBVolSetVal(share_parameterCfgData_st.estaMagnCfg_st.BuildVolt_initial,20))
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_BVolSetVal])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
        }
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

/**
 * @brief 开始采集数据
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_5st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_TRUE;
    // 波形
	ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_Waveform,BitOperaOpt_Set);
    // 期间数据
	ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_BuildUpVoltage,BitOperaOpt_Set);

    ActInform |= WorkflowOutRequestBit_WaveRequest;

    SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

/**
 * @brief 转动预设时间
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_6st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_TRUE;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        if(PidReqBVolSetTime(motorFOR,0.1))
            SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
    }
    else
    {
        // uint8_t FilterTable[]={RawAlarm_DI_BVOL_HLimit,RawAlarm_DI_BVOL_LLimit,0xFF};
        // /* 屏蔽告警 */
        // FilterOverShutDownAlarm(TempOutAlarm,FilterTable);
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(GetShareBufferDataContent(App_Sem_WORKFLOW, CfgOperaType_OSSem, &ShareBuffer_SharePIDOrderSta[ShareInOrderSta_BVolSetTime])==PIDOrderSta_DEF_SUCCESS)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
            ActInform |=WorkflowOutRequestBit_NextMode;
        }
        // 也可能转到限位
        if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_DI_BVOL_HLimit)||\
            GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_DI_BVOL_LLimit))
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}


/**
 * @brief 测量调压前后的电压并等待设置时间
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_7st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_TRUE;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        AVRBeforeVolt_nwait = ACAD_RTGlobalData.BuildVoltData.BuildVoltBEF;
        AVRAfterVolt_nwait = ACAD_RTGlobalData.BuildVoltData.BuildVoltAFT;
        SMTimerCounter = (float)share_parameterCfgData_st.estaMagnCfg_st.BVMotorStopTime/1000 * WorkFlowTimingFreq;
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }
        if(SMTimerCounter==0)
        {
            SetNextNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_Maximum);
            ActInform |=WorkflowOutRequestBit_NextMode;
        }
    }

    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}
/**
 * @brief 测量等待后调压前后的电压，
 * 
 * @param TempWorkflow 
 * @param TempWFStep 
 * @param TempWFPeriod 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_8st(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE,WFStaChange = DEF_TRUE;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        AVRBeforeVolt_wait = ACAD_RTGlobalData.BuildVoltData.BuildVoltBEF;
        AVRAfterVolt_wait = ACAD_RTGlobalData.BuildVoltData.BuildVoltAFT;
    }
    else
    {
        /* AlarmShutDown     */
        if(ShutDownAlarm_WorkStepHandle(&TempWFStep,&TempWFPeriod,&WFStaChange,GenFaultWay_ALL))
        {
            UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
            return (ActInform);
        }

        // 建压成功
        if((AVRBeforeVolt_wait-AVRBeforeVolt_nwait)>share_parameterCfgData_st.estaMagnCfg_st.BVSucsuss_threshold)
        {
            if(AVRAfterVolt_nwait>share_parameterCfgData_st.estaMagnCfg_st.BuildVolt_PreStandard)
            {
                //产生AVR建压值过高告警
                ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_VOLVAL_High,BitOperaOpt_Set);
            }
            share_EstaMagnData_st.baseData_st.EstablishMagnetic_MinVolt = AVRAfterVolt_nwait;// 记录第1次降压后的电压为AVR的最低建压值
            share_EstaMagnData_st.baseData_st.EstablishMagnetic_Frequency = ACAD_RTGlobalData.BuildVoltData.BuildVoltFreq;
            SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_endStep);
        }
        else
        {   
            // 调节到最大限位了
            if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_DI_BVOL_HLimit))
            {
                if(AVRAfterVolt_wait>share_parameterCfgData_st.estaMagnCfg_st.BuildVolt_PreStandard)
                {
                    //产生AVR建压值过高告警
                    ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_VOLVAL_High,BitOperaOpt_Set);
                }
                else
                {
                    //产生测试发电机剩磁电压低告警
                    ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_REMVOL_Low,BitOperaOpt_Set);
                }

                SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_endStep);
            }
            // // 未知原因可能也到达低限位
            // else if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_DI_BVOL_LLimit))
            // {
            //     ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_BVOL_LLimit,BitOperaOpt_Set);
            //     SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_endStep);
            // }
            else if(abs(AVRAfterVolt_wait-AVRBeforeVolt_wait)<0.1)
            {
                // 调压器或电机故障警告 调节失效
                ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_RawAlarm,RawAlarm_BVOL_Invalid,BitOperaOpt_Set);
                SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_endStep);
            }
            else
            {
                //继续循环
                SetNewWorkFlowStepStatus(&TempWFStep,&TempWFPeriod,&WFStaChange,BuildUpVoltageStep_6ndStep);
            }
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}



/**
 * @brief 建压流程
 * 结束建压流程
 * 
 * @param TempWorkflow 
 * @param TempOutAlarm 
 * @return uint8_t 
 */
uint8_t BuildUpVoltageStep_end(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod,  uint8_t *TempOutAlarm)
{    
    uint8_t ActInform = DEF_FALSE,WFStaChange= DEF_FALSE;
    uint8_t WorkflowMode = TempWorkflow;
    if (TempWFPeriod == WorkflowPeriod_Entry)
    {
        SetNewWorkFlowPeriodStatus(&TempWFPeriod,&WFStaChange,WorkflowPeriod_1st);
        CurrProcCountdownEnable=DEF_FALSE;
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_WaveformEnd,BitOperaOpt_Set);
        ModifyFlagShareBufferDataContent(NULL, CfgOperaType_None, FlagShareBuffer_WFWave, WFWave_BuildUpVoltageEnd,BitOperaOpt_Set);
        ActInform |= WorkflowOutRequestBit_WaveRequest;
    }
    else
    {
        FlowDataStatus |= (1<<WorkflowMode_BuildUpVoltage);
        if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,FlagShareBuffer_GenWorkingSta,GenWorkStaOrd_AlarmShutDown))
        {
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_StopPriMover);
        }
        else
        {
            //正常停机计算数据
            SetNewWorkflowModeStatus(&TempWorkflow,&TempWFStep,&TempWFPeriod,&WFStaChange,WorkflowMode_Ending);
            if(TemCount)
            {
                share_EstaMagnData_st.baseData_st.GenExci_volt = TemSUM_ExcitationVolt/TemCount;
                share_EstaMagnData_st.baseData_st.GenExci_current = TemSUM_ExcitationCurr/TemCount;
            }
            // 断开调压器到AVR励磁电源和发电机之间
            ModifyFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,RelayOutput_BUILD_VOL_SW,BitOperaOpt_Clr);
            ActInform |= WorkflowOutRequestBit_RelayOut;
            WaveSetFlowDataStatus(WorkflowMode);
        }
    }
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

/* 工作流程里面的步骤 */
typedef uint8_t (*WorkFlowStepType)(uint8_t TempWorkflow,uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm);
static const WorkFlowStepType BuildUpVoltageStepTable[] = {
    (WorkFlowStepType)BuildUpVoltageStep_1st        ,
    (WorkFlowStepType)BuildUpVoltageStep_2st        ,
    (WorkFlowStepType)BuildUpVoltageStep_3st        ,
    (WorkFlowStepType)BuildUpVoltageStep_4st        ,
    (WorkFlowStepType)BuildUpVoltageStep_5st        ,
    (WorkFlowStepType)BuildUpVoltageStep_6st        ,
    (WorkFlowStepType)BuildUpVoltageStep_7st        ,
    (WorkFlowStepType)BuildUpVoltageStep_8st        ,
    (WorkFlowStepType)BuildUpVoltageStep_end        ,
};

/**
 * @brief 
 * 
 * @param TempWorkflow 当前工作流程
 * @param TempWFStep 流程内第几步
 * @param TempOutAlarm 告警
 * @return uint8_t 
 */
uint8_t WorkFlowHandling_BuildUpVoltage(uint8_t TempWorkflow, uint8_t TempWFStep,uint8_t TempWFPeriod, uint8_t *TempOutAlarm)
{
    uint8_t ActInform = DEF_FALSE, WFStaChange = DEF_FALSE;

	ActInform |= (*BuildUpVoltageStepTable[TempWFStep])(TempWorkflow,TempWFStep,TempWFPeriod, TempOutAlarm);

    /************************************/
    /* Update Workflow--Step and Mode */
    UpdateWorkflowModeStatus(&TempWorkflow, &TempWFStep,&TempWFPeriod, &WFStaChange);
    return (ActInform);
}

#endif